﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using EasierRdp.Base;
using EasierRdp.Models;
using EasierRdp.Util;

namespace EasierRdp.Models
{
    class RdpRoot : IGroupContainer
    {
        private readonly string _configFile;
        private readonly XElement _savedSettings;
        private readonly RdpAllServers _allServers = new RdpAllServers();
        private readonly RdpAllSettings _allSettings = new RdpAllSettings();
        private readonly RdpAllUsers _allUsers = new RdpAllUsers();
        private readonly RdpAllSessions _allSessions = new RdpAllSessions();
        private readonly IList<RdpGroup> _groups = new List<RdpGroup>();

        public const string
            AttributeTreeViewWith = "TreeWidth",
            AttributeName = "Name",
            ElementName = "EasierRdp";

        public string Name { get { return "EasierRdp"; }}

        public IList<RdpGroup> Groups { get { return _groups; } }

        public int TreeViewWidth { get; set; }

        public IEnumerable<INode> Children
        {
            get
            {
                foreach (var item in _groups)
                {
                    yield return item;
                }
                yield return _allUsers;
                yield return _allServers;
                yield return _allSettings;
                yield return _allSessions;
            }
        }

        public bool Expanded { get; set; }

        public RdpRoot(string configFile)
        {
            _configFile = configFile;
            RdpContext.Initialize();
            Expanded = true;
            using (var file = File.OpenText(configFile))
            {
                var content = file.ReadToEnd();
                if (content.Length > 0)
                {
                    _savedSettings = XElement.Parse(content);
                }
                if (_savedSettings != null)
                {
                    var width = _savedSettings.GetNullableInt(AttributeTreeViewWith);
                    TreeViewWidth = width ?? 150;
                    _groups.FromXml(_savedSettings, RdpGroup.ElementName);
                    _allServers.FromXml(_savedSettings, RdpAllServers.ElementName);
                    _allUsers.FromXml(_savedSettings, RdpAllUsers.ElementName);
                    _allSettings.FromXml(_savedSettings, RdpAllSettings.ElementName);
                }
            }
        }

        public bool SaveSettings()
        {
            if (File.Exists(_configFile))
            {
                File.Delete(_configFile);
            }
            using(var file = new StreamWriter(File.OpenWrite(_configFile)))
            {
                file.Write(ToXml().ToString());
            }
            return true;
        }

        public XElement ToXml()
        {
            return new XElement(ElementName
                , new XAttribute(AttributeTreeViewWith, TreeViewWidth)
                , _allServers.ToXml(), _allSettings.ToXml(), _allUsers.ToXml(), _groups.ToXml());
        }
    }
}
